//
//  ManualViewController.m
//  FruitsWholesaler
//
//  Created by sgone-2 on 15/11/17.
//  Copyright © 2015年 sgone. All rights reserved.
//

#import "ManualViewController.h"
#import "AFNetworking.h"
//#import "AFNetworkTool.h"
#import "MJRefresh.h"
//上传图片到七牛
#import "QiniuSDK.h"
#import "MBProgressHUD+MJ.h"
#import "Fruits2.h"
#import "Fruits1.h"
//添加地址区域
#import "HZAreaPickerView.h"

#import "PreviewViewController.h"



#define NUMBERS @"0123456789.\n"
@interface ManualViewController () <UITextFieldDelegate,UITextViewDelegate,UIActionSheetDelegate,UIPickerViewDelegate,UINavigationControllerDelegate,UIImagePickerControllerDelegate,UIPickerViewDataSource,HZAreaPickerDelegate,UITextViewDelegate>






@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property(nonatomic,strong) UITextField *textName;
@property(nonatomic,strong) UITextField *textFenlei;
@property(nonatomic,strong) UITextField *textChandi;
@property(nonatomic,strong) UITextField *textNumber;
@property(nonatomic,strong) UITextField *textUnit;
@property(nonatomic,strong) UITextField *textHeavy;
@property(nonatomic,strong) UITextField *textPrice;
@property(nonatomic,strong) UITextField *textJunjia;
//@property(nonatomic,strong) UITextField *textKucun;
@property(nonatomic,strong) UITextField *textphone;
@property(nonatomic,strong) UITextView *textDescribe;
@property(nonatomic,strong) UITextField *textShangjia;

//规格
@property(nonatomic,strong) NSString *guiGe;

//网路请求
@property(nonatomic,strong)AFHTTPSessionManager *manager;


////上传七牛服务器
@property (nonatomic, strong) QNUploadManager *uploader;
//获得七牛的token的数据
@property(nonatomic,strong) NSString *QNtoken;

//上传到七牛服务器所进行的拼接字符串
@property (nonatomic, copy) NSString *nameKey;
//上传到七牛形成的网络路径
@property (copy, nonatomic)NSString *filePath;
//设置显示的图片
@property (nonatomic,strong) UIImageView *imageView;





//获得分类pickView获得的处理
//生成一个PickView工具
@property(nonatomic,strong) UIPickerView *pickView1;
//蒙版
@property(nonatomic,strong) UIView *orderMskView;
@property(nonatomic,strong) NSMutableArray *orderDatas;
@property(nonatomic) NSInteger currentSelectInCom0;
//上传到服务器的ID2的子的分类号码
@property(nonatomic,strong) NSString *ID2;
//上传服务器的分类名称
@property(nonatomic,strong) NSString *productName;

//地址信息
@property (strong,nonatomic) NSString *areaValue;

//获取得到省市
@property (nonatomic,strong)NSString *stateValue;
@property (strong,nonatomic)NSString *cityValue;


@property (strong,nonatomic) HZAreaPickerView *locatePicker;
//添加城市pickView蒙版
@property (strong,nonatomic) UIView *orderMskView2;



//value将值传递到服务器,需要判断上下架1上架 0下架,上下架操作
//状态值的传递
@property(nonatomic,strong) NSString *value;
@property(nonatomic,strong) UIPickerView *pickView2;


//textView label的待位符
@property(nonatomic,strong) UILabel *placeholder;



@end

@implementation ManualViewController





//七牛上传图片的状态成功或者是失败
{

    //imagePickerView进行处理
    
    UIImagePickerController *_ctrl;
    
    
    
    //地址pickView处理
    NSString *_areaValue;
    NSString *_cityValue;
    HZAreaPickerView *_locatePicker;
    
}






//隐藏tabbar
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.hidesBottomBarWhenPushed = YES;
        
    }
    return self;
}




//afnetworking网络请求数据
- (AFHTTPSessionManager *)manager
{
    if (_manager == nil) {
        _manager  = [AFHTTPSessionManager manager];
    }
    return _manager;
    
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.backBtn.hidden = NO;
    self.titleLabel.text = @"添加商品";
    self.orderDatas = [[NSMutableArray alloc]initWithCapacity:0];
    self.currentSelectInCom0 = 0;
    
 
    
    //添加底部button界面操作
    
    [self initBottom];
    
    
    //获取七牛token
    [self getQiniuToken];
    
  
    //异步加载数据
   dispatch_async(dispatch_get_main_queue(), ^{
    //获取分类信息
    
    [self getFenlei];
    
    });
 
    
}



#pragma mark 设置底部bottom
-(void)initBottom

{

    UIButton *proBtn = [[UIButton alloc]initWithFrame:CGRectMake(0,ViewHeight -64 , 90, 64)];
    [proBtn setTitle:@"预  览" forState:UIControlStateNormal];
    proBtn.titleLabel.font = [UIFont systemFontOfSize:18];
    [proBtn setTitleColor:BenSe_COLOR forState:UIControlStateNormal];
    [self.view addSubview:proBtn];
    [proBtn addTarget:self action:@selector(pro:) forControlEvents:UIControlEventTouchUpInside];
    
 
    
    UIButton *btn2 = [[UIButton alloc]initWithFrame:CGRectMake(95, ViewHeight - 64+10, ViewWidth-95-10, 44)];
    btn2.backgroundColor = BenSe_COLOR;
    [self.view addSubview:btn2];
    [btn2 setTitle:@"确   认" forState:UIControlStateNormal];
    [btn2 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

    btn2.layer.cornerRadius = 3.0;
    
    
    [btn2 addTarget:self action:@selector(sure:) forControlEvents:UIControlEventTouchUpInside];
    
    



}



#pragma mark 预览
-(void)pro:(id)sender
{

    
    if (self.nameKey.length == 0 || self.textName.text.length == 0 || self.textFenlei.text.length == 0 || self.textChandi.text.length == 0 || self.textNumber.text.length == 0 || self.textUnit.text.length == 0 || self.textHeavy.text.length == 0 || self.textPrice.text.length == 0 || self.textJunjia.text.length == 0  || self.textphone.text.length == 0 || self.textDescribe.text.length == 0) {
        
        
        [self.view makeToast:@"请完善商品信息"];
        return;
    }
    
    

    
    self.guiGe =  [NSString stringWithFormat:@"%@ %@ %@",self.textNumber.text,self.textUnit.text, self.textHeavy.text];
    PreviewViewController *preView = [[PreviewViewController alloc]initWithNibName:nil bundle:nil];
    

    
    //传值
    preView.nameKey = [NSString stringWithFormat:@"http://fruitsimage.qiniudn.com/%@",self.nameKey];
    
    preView.textName = self.textName.text;
    preView.textFenlei = self.textFenlei.text;
    preView.textChandi = self.textChandi.text;
    
    preView.guiGe = self.guiGe;
    preView.textPrice = self.textPrice.text;
    preView.textJunjia  = self.textJunjia.text;
//    preView.textKucun = self.textKucun.text;
    preView.textphone = self.textphone.text;
    preView.textDescribe = self.textDescribe.text;
    
    
    
    
    [self.navigationController pushViewController:preView animated:YES];



}


#pragma mark 确定，上传数据到服务器上
-(void) sure:(id)sender
{
    //规格
    
    self.guiGe =  [NSString stringWithFormat:@"%@ %@ %@",self.textNumber.text,self.textUnit.text, self.textHeavy.text];
//    NSLog(@"333333333  %@ %@   %@ %@  %@",self.textChandi.text,self.textJunjia.text,self.guiGe,self.cityValue,self.stateValue);

    
    
    if (self.nameKey.length == 0) {
        
        [self.view makeToast:@"请上传商品照片"];
        return;
    }
    
    if (self.textName.text.length == 0) {
        [self.view makeToast:@"名称不能为空"];
        return;
    }
    if (self.textFenlei.text.length == 0) {
        
        [self.view makeToast:@"分类不能为空"];

        return;
    }
    if (self.textChandi.text.length == 0) {
        [self.view makeToast:@"产地不能为空"];
        return;
    }
    if (self.textNumber.text.length == 0) {
        [self.view makeToast:@"数量不能为空"];
        return;
    }
    if (self.textUnit.text.length == 0) {
        [self.view makeToast:@"单位不能为空"];
        return;
    }
    if (self.textHeavy.text.length == 0) {
        [self.view makeToast:@"重量不能为空"];
        return;
    }
    if (self.textPrice.text.length == 0) {
        [self.view makeToast:@"单价不能为空"];
        return;
    }
    if (self.textJunjia.text.length == 0) {
        [self.view makeToast:@"均价不能为空"];
        return;
    }
//    if (self.textKucun.text.length == 0) {
//        [self.view makeToast:@"库存不能为空"];
//        return;
//    }
    if (self.textphone.text.length == 0) {
        [self.view makeToast:@"联系电话不能为空"];
        return;
    }
    if (self.textDescribe.text.length == 0) {
        [self.view makeToast:@"描述不能为空"];
        return;
    }
    
    
    
    
    
    
    
    NSString*urlStr = [NSString stringWithFormat:@"%@add_product",SERVER_ADDRESS2];
    NSDictionary *params = @{@"wTel" : self.dataHandler.phone , @"uToken" : self.dataHandler.uToken , @"product_name" : self.textName.text , @"unit_price" : self.textPrice.text , @"product_unit" : self.guiGe ,@"avg_price" : self.textJunjia.text , @"category" : self.ID2 , @"product" :  self.textFenlei.text , @"province" :self.stateValue , @"city" :  self.cityValue , @"image" : self.nameKey  ,@"tel" :  self.textphone.text  ,@"description" : self.textDescribe.text };

    [MBProgressHUD showHUDAddedTo:self.tableView animated:YES];
    
    
    [self.manager POST:urlStr parameters:params success:^(NSURLSessionDataTask *task, id responseObject) {
        if ([responseObject[@"error_flag"] intValue]  == 0) {
            
//            NSLog(@"上传商品成功");
            
            
            [MBProgressHUD hideHUDForView:self.tableView animated:YES];
            UIAlertView *alter = [[UIAlertView alloc]initWithTitle:@"提示" message:@"上传成功" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
            [alter show];
            [self.navigationController popViewControllerAnimated:YES];
            
            
            
        }
        else
        {
        
        
            [MBProgressHUD hideHUDForView:self.tableView animated:YES];
            NSString *str = [NSString stringWithFormat:@"%@",responseObject[@"result_msg"]];
            
            UIAlertView *alter = [[UIAlertView alloc]initWithTitle:@"提示" message:str delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
            [alter show];
            
        }
        
        
        
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
          [MBProgressHUD hideHUDForView:self.tableView animated:YES];
        UIAlertView *alter = [[UIAlertView alloc]initWithTitle:@"提示" message:@"上传服务器失败" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
        [alter show];
        
        
    }];



}




#pragma  mark获取七牛的token


//通过七牛上传图片
- (void)getQiniuToken
{
    
//    [MBProgressHUD showHUDAddedTo:self.tableView animated:YES];
//    [MBProgressHUD showMessage:@"加载中"];
    NSString *str = [NSString stringWithFormat:@"http://app.sgone.cn/appapi_v4/wholesaler_common/get_private_storage_token"];
    NSURL *url = [NSURL URLWithString:str];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    NSOperationQueue *queue = [NSOperationQueue mainQueue];
    
    [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        if (connectionError || data == nil)
        {
            return ;
        }
        //解析服务器返回来的json数据
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
        
        NSString *token = [[dict objectForKey:@"data"] objectForKey:@"upToken"];
        NSNumber *expire = [[dict objectForKey:@"data"] objectForKey:@"expire"];
        
        
        self.QNtoken = token;
        
//        NSLog(@"输出的responsedata是  %@",dict) ;
        
        if (token != nil)
        {
//            NSLog(@"-----------------%@商品头像获取七牛token成功：%@     %@",token,expire,dict);
            
            
           
            
        }
        
    }];
    
    
}





#pragma mark获取商品分类


-(void) getFenlei

{
    
 
  
//    [MBProgressHUD showHUDAddedTo:self.tableView animated:YES];
    
    NSString *urlStr = [NSString stringWithFormat:@"%@product_category",SERVER_ADDRESS];
//    NSDictionary *params = nil;
    [self.manager POST:urlStr parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        
        if ([responseObject[@"error_flag"] intValue] == 0) {
            
            
//            NSLog(@"ttttt  %@",responseObject);
            
            //将数据传递到下一个方法里进行操作
     
            [self setupDataFormIdct:responseObject];
            
            
            //进行pickView以及蒙版的生成以及操作
            
            if (self.orderMskView == nil) {
                
                self.orderMskView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, [UIScreen mainScreen].bounds.size.height  )];
            }
                self.orderMskView.backgroundColor = [UIColor clearColor];
            
                [self.view addSubview:self.orderMskView];
                //添加清除操作
                UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(fingerTapped:)];
                singleTap.cancelsTouchesInView = NO;
                [self.orderMskView addGestureRecognizer:singleTap];
                if (self.pickView1 == nil)
            {
                    
            self.pickView1 = [[UIPickerView alloc]initWithFrame:CGRectMake(0,ViewHeight - 213, ViewWidth, 220)];
               
            }
            [self.pickView1 setTag:100];
            
            
            self.pickView1.backgroundColor = [UIColor whiteColor];
            self.pickView1.delegate = self;
            [self.orderMskView addSubview:self.pickView1];
            [self.pickView1 reloadAllComponents];
            
            
            
            self.pickView1.hidden = YES;
            self.orderMskView.hidden = YES;
            
            
        }
        
        
        else
        {
        
             [MBProgressHUD hideHUDForView:self.tableView animated:YES];
            NSString *str = [NSString stringWithFormat:@"%@",responseObject[@"result_msg"]];
            
            UIAlertView *alter = [[UIAlertView alloc]initWithTitle:@"提示" message:str delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
            [alter show];

        
        
        
        }
        
        
    } failure:^(NSURLSessionDataTask *task, NSError *error)
    {
        
        [MBProgressHUD hideHUDForView:self.tableView animated:YES];
        UIAlertView *alter = [[UIAlertView alloc]initWithTitle:@"提示" message:@"请检查网络" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
        [alter show];
        
        
    }];
    






}


#pragma mark 分类获取网路数据处理
-(void)setupDataFormIdct:(NSDictionary*)dict
{

    
    
//    NSLog(@"jjjjjjj %@",[dict[@"data"] class]);
    
    
    if ([dict[@"data"] isKindOfClass:[NSArray class]]) {
    
        NSArray *menu = dict[@"data"];
        
        [menu enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop)

           {
            
            NSArray *menuArray = obj;
               
            Fruits1 *fruits1 = [[Fruits1 alloc]initWithDataDic:menuArray];
               
            [self.orderDatas addObject:fruits1];
               
        }];
        [self.pickView1 reloadAllComponents];

       [MBProgressHUD hideHUDForView:self.tableView animated:YES];
        

    }
    else
    {
   [MBProgressHUD hideHUDForView:self.tableView animated:YES];
    
//        NSLog(@"获取网络数据类型出错");
    
    }
    
    

}










-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    return 1;

}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

  
    return 9;
    


}


//在header处理tabelViewCell重用问题

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{


    return 0.01;


}


-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{

    return 64;

}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

    if (indexPath.row == 8 ) {
        return 100;
    }
    
    else if (indexPath.row == 0)
    {
    
        return ViewWidth / 2 +10;
    
    }

    else
    {
        return 44;
    
    }
    
    


}








-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

   static NSString *CellIdentifier  =  @"Cell";
//    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
//    if (cell == nil) {
//    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    
    UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    
    
   cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    
    

        
        
//        
//    }

    
    UILabel *lbl = [[UILabel alloc]initWithFrame:CGRectMake(95, 0, 10, 46)];
    lbl.text = @"|";
    lbl.textColor = [UIColor grayColor];
    [cell addSubview:lbl];
    
    //设置白色的颜色深度
    cell.textLabel.textColor = [UIColor colorWithWhite:0.5 alpha:1];
    cell.textLabel.font = [UIFont systemFontOfSize:18];
   
    
    if (indexPath.section == 0) {
        
    
    if (indexPath.row == 0) {
      
        
        
        

        
        
        
        
        cell.backgroundColor = BACKGROUND2;
        
        if (self.imageView == nil)
        {
        self.imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, ViewWidth , ViewWidth /2 )];
        self.imageView.image = [UIImage imageNamed:@"addPro"];
        [cell addSubview:self.imageView];
            
        }
       
        
        
        
        UIButton *btnImage = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, ViewWidth, ViewWidth / 2)];
 
        
        [btnImage addTarget:self action:@selector(imagePick:) forControlEvents:UIControlEventTouchUpInside];
        [cell addSubview:btnImage];
        
        
    }
    
    
   else if (indexPath.row == 1) {
        cell.textLabel.text = @"商品名";
        if (self.textName == nil) {
       
        self.textName = [[UITextField alloc]initWithFrame:CGRectMake(115, 0, ViewWidth *0.6, 44)];
            self.textName.font = [UIFont systemFontOfSize:16];
            self.textName.textColor = [UIColor lightGrayColor];
            self.textName.placeholder = @"输入商品名称";
            [cell addSubview:self.textName];
            
            self.textName.delegate = self;
            //显示键盘变为完成
            self.textName.returnKeyType = UIReturnKeyDone;
            //cleanButton
            self.textName.clearButtonMode =  UITextFieldViewModeWhileEditing;
            
            
        }
    

    
        
        
        
    }
    else if (indexPath.row == 2)
    {
      cell.textLabel.text = @"分类";
      cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;//样式
    
        if (self.textFenlei == nil) {
    
        self.textFenlei = [[UITextField alloc]initWithFrame:CGRectMake(115, 0, ViewWidth *0.6, 44)];
            
            self.textFenlei.font = [UIFont systemFontOfSize:16];
            self.textFenlei.textColor = [UIColor lightGrayColor];
            self.textFenlei.placeholder = @"添加商品分类";
            [cell addSubview:self.textFenlei];
            
            self.textFenlei.userInteractionEnabled = NO;
            self.textFenlei.delegate = self;
            //显示键盘变为完成
            self.textFenlei.returnKeyType = UIReturnKeyDone;

            
            
  
  
            
        }
        
    
    }
    else if (indexPath.row == 3)
    {
     cell.textLabel.text = @"产       地";
     cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;//样式
        if (self.textChandi == nil) {
        
          self.textChandi = [[UITextField alloc]initWithFrame:CGRectMake(115, 0, ViewWidth *0.6, 44)];
        
            self.textChandi.font = [UIFont systemFontOfSize:16];
            self.textChandi.textColor = [UIColor lightGrayColor];
            self.textChandi.placeholder = @"请选择商品产地";
            [cell addSubview:self.textChandi];
            self.textChandi.userInteractionEnabled = NO;
            self.textChandi.delegate = self;
        self.textChandi.returnKeyType = UIReturnKeyDone;
        }
        
        
        //赋值操作
        self.textChandi.text = _areaValue;
       
       
        
    }
    
    else if (indexPath.row == 4)
    {
    
    cell.textLabel.text = @"规       格";
    //
        
        if (self.textNumber == nil) {
            self.textNumber = [[UITextField alloc]initWithFrame:CGRectMake(110, 4, 64, 36)];
            self.textNumber.delegate = self;
            self.textNumber.returnKeyType = UIReturnKeyDone;
            [cell addSubview:self.textNumber];
            self.textNumber.placeholder = @"数量";
            self.textNumber.font = [UIFont systemFontOfSize:16];
            self.textNumber.textColor = [UIColor lightGrayColor];
            //设置textField的边框
            self.textNumber.layer.borderWidth = 1.0f;
            self.textNumber.layer.cornerRadius = 5;
            self.textNumber.layer.borderColor = [UIColor lightGrayColor].CGColor;
            self.textNumber.textAlignment = NSTextAlignmentCenter;
            self.textNumber.clearButtonMode = UITextFieldViewModeWhileEditing;
            
        }
        

        
        if (self.textUnit == nil) {
            self.textUnit = [[UITextField alloc]initWithFrame:CGRectMake(110+64+10, 4, 64, 36)];
          
            self.textUnit.delegate = self;
            self.textUnit.returnKeyType = UIReturnKeyDone;
            [cell addSubview:self.textUnit];
            self.textUnit.placeholder = @"单位";
            self.textUnit.font = [UIFont systemFontOfSize:16];
            self.textUnit.textColor = [UIColor lightGrayColor];
            
            //设置textField的边框
            self.textUnit.layer.borderWidth = 1.0f;
            self.textUnit.layer.cornerRadius = 5;
            self.textUnit.layer.borderColor = [UIColor lightGrayColor].CGColor;
            self.textUnit.textAlignment = NSTextAlignmentCenter;
            self.textUnit.clearButtonMode = UITextFieldViewModeWhileEditing;
        }
        

      
        
        
        if (self.textHeavy == nil) {
            self.textHeavy = [[UITextField alloc]initWithFrame:CGRectMake(110+64*2+10*2, 4, 64, 36)];
           
            self.textHeavy.delegate = self;
            self.textHeavy.returnKeyType = UIReturnKeyDone;
            [cell addSubview:self.textHeavy];
            self.textHeavy.placeholder = @"约重量";
            self.textHeavy.font = [UIFont systemFontOfSize:16];
            self.textHeavy.textColor = [UIColor lightGrayColor];
            //设置textField的边框
            self.textHeavy.layer.borderWidth = 1.0f;
            self.textHeavy.layer.cornerRadius = 5;
            self.textHeavy.layer.borderColor = [UIColor lightGrayColor].CGColor;
            self.textHeavy.textAlignment = NSTextAlignmentCenter;
            self.textHeavy.clearButtonMode = UITextFieldViewModeWhileEditing;
            
            
        }
        

   
        
        
        self.guiGe =  [NSString stringWithFormat:@"%@%@%@",self.textNumber.text,self.textUnit.text, self.textHeavy.text];
        
        
        
        
        
        
        
    }
    else if (indexPath.row == 5)
    {
    
    cell.textLabel.text = @"单       价";
       
        if (self.textPrice == nil) {
  
        self.textPrice = [[UITextField alloc]initWithFrame:CGRectMake(115, 0, ViewWidth *0.6, 44)];
            
            self.textPrice.font = [UIFont systemFontOfSize:16];
            self.textPrice.textColor = [UIColor lightGrayColor];
            self.textPrice.placeholder = @"请输入单价";
            [cell addSubview:self.textPrice];
            self.textPrice.delegate = self;
            self.textPrice.returnKeyType = UIReturnKeyDone;
            self.textPrice.clearButtonMode = UITextFieldViewModeWhileEditing;
        }
       
        
        UILabel *lbl = [[UILabel alloc]initWithFrame:CGRectMake(ViewWidth *0.6 + 110+5, 0, 20, 44)];
        lbl.font = [UIFont systemFontOfSize:15];
        lbl.text = @"元";
        lbl.textColor = [UIColor grayColor];
        [cell addSubview:lbl];
        
        
        
        

    }
    else if (indexPath.row == 6)
    {
    
    cell.textLabel.text = @"均       价";
        
        if (self.textJunjia == nil) {
    
        self.textJunjia  = [[UITextField alloc]initWithFrame:CGRectMake(115, 0, ViewWidth *0.5, 44)];
            
            self.textJunjia.font = [UIFont systemFontOfSize:16];
            self.textJunjia.textColor = [UIColor lightGrayColor];
            self.textJunjia.placeholder = @"如: 5元/斤";
            [cell addSubview:self.textJunjia];
            self.textJunjia.delegate = self;
            self.textJunjia.returnKeyType = UIReturnKeyDone;
            self.textJunjia.clearButtonMode = UITextFieldViewModeWhileEditing;

        }
        
        
        UILabel *lbl = [[UILabel alloc]initWithFrame:CGRectMake(ViewWidth *0.5 + 110+15, 0, 48, 44)];
        lbl.font = [UIFont systemFontOfSize:15];
        lbl.text = @" 元/斤";
        lbl.textColor = [UIColor grayColor];
        [cell addSubview:lbl];
    

    }

    else if (indexPath.row == 7)
    {
    
    cell.textLabel.text = @"联系电话";
        
        if (self.textphone == nil) {
       
        self.textphone  = [[UITextField alloc]initWithFrame:CGRectMake(115, 0, ViewWidth *0.6, 44)];
         
            
            self.textphone.font = [UIFont systemFontOfSize:16];
            self.textphone.textColor = [UIColor lightGrayColor];
            self.textphone.placeholder = @"请输入手机号";
            [cell addSubview:self.textphone];
            self.textphone.delegate = self;
            self.textphone.returnKeyType = UIReturnKeyDone;
            self.textphone.clearButtonMode = UITextFieldViewModeWhileEditing;
            
            self.textphone.text = self.dataHandler.phone;
            

        }
       
 
    }
    
    else 
    {
        
        
    cell.textLabel.text = @"商品描述";
//    lbl.frame = CGRectMake(100, 0, 10, 100);
        
        if (self.textDescribe == nil) {
   
        self.textDescribe = [[UITextView alloc]initWithFrame:CGRectMake(110, 0, ViewWidth *0.6, 100-1)];
        
            [cell addSubview:self.textDescribe];
        
        
        self.textDescribe.font = [UIFont systemFontOfSize:16];
        self.textDescribe.textColor = [UIColor lightGrayColor];
        
            self.placeholder  = [[UILabel alloc]initWithFrame:CGRectMake(115, 0, 160, 30)];
            self.placeholder.font = [UIFont systemFontOfSize:15];
            self.placeholder.textColor = [UIColor lightGrayColor];
            self.placeholder.text = @"给商品一个介绍吧";
            [cell addSubview:self.placeholder];
            
            self.textDescribe.delegate = self;
            //将键盘变成完成操作
            self.textDescribe.returnKeyType = UIReturnKeyDone;
           

        
        }
        

      

    }

    }
    
    else
    {
    
    
    }
  return cell;

}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{


    if (indexPath.row == 2) {
     
       
        
        
        self.pickView1.hidden = NO;
        self.orderMskView.hidden = NO;
        
    }
    
    else if (indexPath.row == 3)
    {
        


        
    if (self.orderMskView2 == nil)
    {
    
        self.orderMskView2 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ViewWidth, ViewHeight)];
        self.orderMskView2.backgroundColor = [UIColor clearColor];
        [self.view addSubview:self.orderMskView2];
        
        
        
        //收回pickView操作
        UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(pickViewHide:)];
        
        //设置成NO便是当前空间响应后会传播到其他控件上，默认为YES
        tapGes.cancelsTouchesInView = NO;
        [self.orderMskView2 addGestureRecognizer:tapGes];
    
    }
        
 

        
    if (self.locatePicker == nil)
    {
            
    self.locatePicker = [[HZAreaPickerView alloc]initWithStyle:HZAreaPickerWithStateAndCityAndDistrict delegate:self];
        
        self.locatePicker.frame = CGRectMake(0, ViewHeight *0.7, ViewWidth, ViewHeight*0.3);
        [self.locatePicker showInView:self.orderMskView2];
    }
   
        self.orderMskView2.hidden = NO;
        self.locatePicker.hidden = NO;
    
    }
  
    else
    {
    
    
    
    
    }





}



#pragma mark 移除maskView以及pickView的操作
-(void)fingerTapped:(UITapGestureRecognizer *)gestureRecognizer

{

    [self.pickView1 setHidden:YES];
    [self.orderMskView setHidden:YES];
    
    
    

}





#pragma mark 取消地址pickView的现实操作
-(void)pickViewHide:(UITapGestureRecognizer *)tap
{

    
//    self.orderMskView2.hidden = YES;
//    self.locatePicker.hidden = YES;
    [self.orderMskView2 removeFromSuperview];
    [self.locatePicker removeFromSuperview];
    
}






#pragma mark  - HZAreaPicker delegate
-(void)pickerDidChaneStatus:(HZAreaPickerView *)picker
{
    if (picker.pickerStyle == HZAreaPickerWithStateAndCityAndDistrict) {
        //传递省市到服务器上
        self.areaValue = [NSString stringWithFormat:@"%@ %@ %@", picker.locate.state, picker.locate.city, picker.locate.district];
        [self.tableView reloadData];
//        NSLog(@"66666  %@  %@  ",picker.locate.state ,picker.locate.city);
        
        self.stateValue = picker.locate.state;
        self.cityValue = picker.locate.city;
        
        
    }
    

    
}




#pragma mark pickViewDataSource协议方法

//指定pickView的组件个数
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{

    if (pickerView.tag == 100) {
        return 2;
    }
    //其他PickView的操作
    else
    {
    
        
        
        return 1;
    
    
    
    }
    


}


//指定pickView中每一列的个数
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{

    if (pickerView.tag == 100) {
        
        NSInteger selectIndeInCom0  = [pickerView selectedRowInComponent:0];
        Fruits1 *selectMenu = _orderDatas[selectIndeInCom0];
        return (component == 0) ?_orderDatas.count:selectMenu.fruits1.count;
        
    }
    else
    {
    
        return 2;
    
    }


}


#pragma mark - UIPickerViewDelegate 协议方法
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component

{

  
    NSString *str1 = nil;

    if (pickerView.tag == 100) {
        
        if (component == 0) {
            Fruits1 *fruits12 = self.orderDatas[row];
            return fruits12.fruits1Name;
           
        }
        Fruits1 *selectMenu = self.orderDatas[self.currentSelectInCom0];
        Fruits2 *fruits22 = selectMenu.fruits1[row];
        return fruits22.fruits2Name;
        
        
    }

    else
    {
    
        return str1;
    
    
    }



}


-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component

{

    if (pickerView.tag == 100) {
        if (component == 0) {
            
            _currentSelectInCom0 = row;
            [pickerView reloadComponent:1];
            [pickerView selectRow:0 inComponent:1 animated:YES];
            
            
        }
        
        
        //获取第一个数组选择的数据获取第二个数组选择的数据
        
        NSInteger selectIndexInCom0 = [self.pickView1 selectedRowInComponent:0];
        NSInteger selectIndexInCom1 = [self.pickView1 selectedRowInComponent:1];
        
        Fruits1*select1 = _orderDatas[selectIndexInCom0];
        Fruits2*select2 = select1.fruits1[selectIndexInCom1];

        
        
        
        //将当前的数值放入到textFenlei中
        
        NSString *str = [NSString stringWithFormat:@"%@ %@",select1.fruits1Name,select2.fruits2Name];
        self.textFenlei.text = str;
        
        
        
        //确定子的ID号
        
        self.ID2 = [NSString stringWithFormat:@"%@",@(select2.menu2ID)];
//        NSLog(@"获得的子分类   %@ %@",self.textFenlei.text,@(select2.menu2ID));
        

        
    }





}






#pragma mark 选择图片传递到七牛服务器

-(void)imagePick:(id)sender
{


    UIActionSheet *actionSheet = [[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"拍照",@"从相册选择", nil];
    
    [actionSheet showInView:self.tableView];
  





}


- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    
    
    if (buttonIndex == 2) {
        
        return;
    }
    
    if(buttonIndex==1)
    {
        
        //调用系统相册
        _ctrl=[[UIImagePickerController alloc]init];
        _ctrl.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;
        _ctrl.delegate=self;
        _ctrl.allowsEditing = YES;
        [self addChildViewController:_ctrl];
        self.tabBarController.tabBar.alpha = 0;
        [self.view addSubview:_ctrl.view];
        self.tabBarController.tabBar.hidden = YES;
        
        
        
        
    }
    else if (buttonIndex==0)
        
    {
        
        
        _ctrl=[[UIImagePickerController alloc]init];
        _ctrl.sourceType=UIImagePickerControllerSourceTypeCamera;
        _ctrl.delegate=self;
        _ctrl.allowsEditing = YES;
        
        [self addChildViewController:_ctrl];
        
        [self.view addSubview:_ctrl.view];
        
        
        
        
    }
}



#pragma mark - ImagePickerController Delegate
//点击取消按钮时候调用
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    
    if (_ctrl)
    {
        [_ctrl.view removeFromSuperview];
        self.navigationController.navigationBar.hidden=YES;
        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:NO];
        
    }
}

//点击确定按钮
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    
    
    
    UIImage *image=[info objectForKey:UIImagePickerControllerEditedImage];
    
    
    self.imageView.image=image;
    
    
    
    self.filePath = [self.dataHandler dataFilePath:@"pifa.jpg"];
    
    NSData *webData = UIImageJPEGRepresentation(self.imageView.image, 0.5);
    [webData writeToFile:self.filePath atomically:YES];
    [self uploadImage];
    [picker.view removeFromSuperview];
    self.navigationController.navigationBar.hidden= YES;
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:NO];
    
  
    
    
}


//网络上传到七牛的操作步骤以及属性
#pragma mark - upload qiniu
- (void)uploadImage{
    
    //获得的商品图片，需要传递给服务器
    self.nameKey = [NSString stringWithFormat:@"pf-%@-%@.jpg",self.dataHandler.phone, [self.dataHandler timeString]];
    
//    NSLog(@"图片  %@    %@  %@",self.nameKey,self.dataHandler.phone,self.dataHandler.uToken);
    
    if (!self.uploader)
    {
        
     self.uploader = [[QNUploadManager alloc]init];
    
    }
    
    [MBProgressHUD showHUDAddedTo:self.tableView animated:YES];
    [self.uploader putFile:_filePath key:self.nameKey token:self.QNtoken complete:^(QNResponseInfo *info, NSString *key, NSDictionary *resp) {
        
    [MBProgressHUD hideAllHUDsForView:self.tableView animated:YES];
//        NSLog(@"%@", info);
//        NSLog(@"七牛返回信息%@", resp);
    } option:nil];
    
 
 

}


#pragma mark点击return时触发的事件,收回键盘

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    
    
    
    [UIView animateWithDuration:0.25 animations:^{
        if (self.view.y == -176) {
            self.view.y = 0;
            [textField resignFirstResponder];
        }
    }completion:^(BOOL finished){
        
         self.tableView.scrollEnabled = YES;
        
        
    }];
    



    return YES;
    
}


//键盘上移操作

#pragma mark 起卖量键盘上移，再次点击键盘收回操作

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    
    if (    [textField isEqual:self.textphone] || [textField isEqual:self.textJunjia] || [textField isEqual:self.textPrice] || [textField isEqual:self.textNumber] || [textField isEqual:self.textUnit] || [textField isEqual:self.textUnit] || [textField isEqual:self.textHeavy])
    {
        [UIView animateWithDuration:0.25 animations:^{
            if (self.view.y == 0) {
                self.view.y = -176;
            }
        }completion:^(BOOL finished){
            //生成pickView
            
            
            self.tableView.scrollEnabled = NO;
    
            
        }];
        
        
    }

    return YES;
}






#pragma mark 键盘上移操作，textviewdelegat操作

//textview上移操作


- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
    
    //当textView进行编辑的时候整个view进行上移
    // if (!iPhone5)
 
        
        [UIView animateWithDuration:0.25 animations:^{
            if (self.view.y == 0) {
                self.view.y = -176;
            }
        }completion:^(BOOL finished){
            self.tableView.scrollEnabled = NO;
        }];

    return YES;
    
    
}

//键盘输入数字操作

//判断是否输入的为数字
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSCharacterSet *cs;
    if( [self.textphone isFirstResponder]  || [self.textPrice isFirstResponder]  || [self.textJunjia isFirstResponder])
    {
        cs = [[NSCharacterSet characterSetWithCharactersInString:NUMBERS] invertedSet];
        NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
        BOOL basicTest = [string isEqualToString:filtered];
        if(!basicTest)
        {
            UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"提示"
                                                            message:@"请输入数字"
                                                           delegate:nil
                                                  cancelButtonTitle:@"确定"
                                                  otherButtonTitles:nil];
            
            [alert show];
            
            //    [self.view makeToast:@"请输入数字"];
            return NO;
        }
    }
    
    
    
    
    //其他的类型不需要检测，直接写入
    return YES;
}







//收回键盘操作,整个界面复原
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if ([text isEqualToString:@"\n"]) {
      

        [UIView animateWithDuration:0.25 animations:^{
            if (self.view.y == -176) {
                self.view.y = 0;
        
            }
        }completion:^(BOOL finished){
            [textView resignFirstResponder];
            self.tableView.scrollEnabled = YES;
            
            
        }];
        
        return NO;
    }
    
    
    return YES;
    
}



-(void)textViewDidChange:(UITextView *)textView
{
    
    
    //给textView输入提示操作
    if ([textView.text length] == 0) {
        [self.placeholder setHidden:NO];
    }else
    {
        [self.placeholder setHidden:YES];
        
    }
    
    
    
    
    
}


@end
